﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace UserApi.Controllers
{

    [Route("api/[Controller]/[action]")]
    [ApiController]
    public class HomeController : ControllerBase
    {

        [HttpGet]
        public IActionResult Index()
        {
            return Content("444444444444444");
        }

        [HttpGet]
        [Authorize]
        public IActionResult GetUser()
        {

            //重要说明，必须引入 IdentityServer4.AccessTokenValidataion 3.0.1版本，Authorize校验才能通过。
            // 引入其他包则不能完成授权和校验
            var a = User.Identity?.IsAuthenticated;
            bool s = User.IsInRole("admin");

            var d= User.Claims.Where(c=>c.Type=="").FirstOrDefault();
            //HttpContext.User

            return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
        }


        /// <summary>
        /// 需要Order权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize("Order")]
        public IActionResult GetOrder()
        {

            //重要说明，必须引入 IdentityServer4.AccessTokenValidataion 3.0.1版本，Authorize校验才能通过。
            // 引入其他包则不能完成授权和校验
            var a = User.Identity?.IsAuthenticated;
            bool s = User.IsInRole("admin");

            var d = User.Claims.Where(c => c.Type == "").FirstOrDefault();

            return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
        }
    }
}
